package com.lyl.quartz.test;

import com.lyl.quartz.jobs.HelloJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.concurrent.TimeUnit;

/**
 * 入门
 * @author lylbell
 * @date 2022-03-29 10:35
 */
public class _03_QuartzTest_MultJobDetail {

    public static void main(String[] args) throws SchedulerException, InterruptedException {

        // 调度器工厂创建默认调度器
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        // 启动调度器
        scheduler.start();

        // 创建job
        JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1", "group1").build();
        JobDetail job2 = JobBuilder.newJob(HelloJob.class).withIdentity("job2", "group1").build();

        // 创建trigger。withIntervalInSeconds：每5秒执行一次。
        // repeatForever：一直重复执行
        // 执行5次。0 5 10 15 20
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever()).build();

        // forJob:指定job
        // 0 3
        Trigger trigger2 = TriggerBuilder.newTrigger()
                .withIdentity("trigger2", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).repeatForever()).build();

        scheduler.scheduleJob(job,trigger);
        scheduler.scheduleJob(job2,trigger2);


        // 执行20秒
        TimeUnit.SECONDS.sleep(3);

        scheduler.shutdown();
    }
}
